﻿using System.Linq;
using System.Windows.Controls;
using Kaesoft.Hospedaje.Entities;
using Telerik.Windows.Controls;
using Kaesoft.Hospedaje.Common;

namespace Kaesoft.Hospedaje.Admin
{
    public partial class AdministracionParametrosCatalogosView : UserControl
    {
        public event System.EventHandler<EventArgs<Kaesoft.Hospedaje.Common.ViewModelBase>> NotifyViewModel;
        private MessageWindowView MessageWindowView;

        public AdministracionParametrosCatalogosView()
        {
            InitializeComponent();
            MessageWindowView = new Common.MessageWindowView();
            Loaded += new System.Windows.RoutedEventHandler(AdministracionParametrosCatalogosView_Loaded);
        }

        void AdministracionParametrosCatalogosView_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (NotifyViewModel != null)
                NotifyViewModel(null, new EventArgs<Common.ViewModelBase>((Kaesoft.Hospedaje.Common.ViewModelBase)busy.DataContext));
            this.DataContext = busy.DataContext;
            ((AdministracionParametrosCatalogosViewModel)busy.DataContext).RefrescarItems += AdministracionParametrosCatalogosView_Refrescar;
        }

        void AdministracionParametrosCatalogosView_Refrescar(object sender, System.EventArgs e)
        {
            gridParametros.Items.Refresh();
            gridCatalogos.Items.Refresh();
        }

        private void RadGridView_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            var vm = (AdministracionParametrosCatalogosViewModel)busy.DataContext;
            switch (((RadGridView)sender).Name)
            {
                case "gridParametros":
                    if (gridParametros.SelectedItems.Count > 0)
                    {
                        vm._parametrosSeleccionados.Clear();
                        var lista = gridParametros.SelectedItems.Cast<Parametro>();
                        vm._parametrosSeleccionados.AddRange(lista.Select(param => param.idParametro));
                        vm.EnabledEliminarParametros = true;
                    }
                    else if (gridParametros.SelectedItems.Count <= 0)
                        vm.EnabledEliminarParametros = false;
                    break;
                case "gridCatalogos":
                    if (gridCatalogos.SelectedItems.Count > 0)
                    {
                        vm._catalogosSeleccionados.Clear();
                        var lista = gridCatalogos.SelectedItems.Cast<Catalogo>();
                        vm._catalogosSeleccionados.AddRange(lista.Select(param => param.idCatalogo));
                        vm.EnabledEliminarCatalogos = true;
                    }
                    else if (gridCatalogos.SelectedItems.Count <= 0)
                        vm.EnabledEliminarCatalogos = false;
                    break;
            }
        }

        private void RadGridView_SelectionChanging(object sender, SelectionChangingEventArgs e)
        {
            var vm = (AdministracionParametrosCatalogosViewModel)busy.DataContext;
            if (vm.IsDirty && ((vm.ListaCatalogos != null && vm.ListaCatalogos.Any(cat => cat.ChangeTracker.State != ObjectState.Unchanged))))
            {
                MessageWindowViewModel msgWindowViewModel = new MessageWindowViewModel("Si desea seleccionar otro tipo de catálogo por favor grabe sus cambios.",
                                                                            MessageBoxButtons.Ok, MessageBoxIcon.Information);
                MessageWindowView.DataContext = msgWindowViewModel;
                msgWindowViewModel.Closing += (msgsender, ev) =>
                {
                    MessageWindowView.Close();
                };
                MessageWindowView.ShowDialog();

                e.Cancel = true;
                e.Handled = true;
            }
        }
    }
}
